package com.ibiaoke.common.configs;

import com.ibiaoke.common.utils.PropertiesUtils;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 系统全局常量工具类
 * 键值请在{@link GlobalConfigsKey} 中定义
 * Created by Administrator on 2017/5/17.
 */

public class GlobalConfigs {
    private static Map<String, String> global = new HashMap<String, String>();

    public static Map<String, String> getGlobal(){
        return global;
    }

    public static String getConfigure(String key){
        key = key.replaceAll("\\.", "_");
        String value = global.get(key);
        return value==null?"":value;
    }

    static{
        init();
    }

    /**
     * Load configs from files.
     */
    private static void init() {
        try {
            Properties[] props = new Properties[1];
            props[0] = PropertiesUtils.loadClassLoader("globalConfig.properties");

            if(props[0]!=null){
                Enumeration en = props[0].keys();
                while(en.hasMoreElements()){
                    String k = en.nextElement().toString();
                    String gk = k;
                    if(k.indexOf(".")>=0){
                        gk = gk.replaceAll("\\.", "_");
                    }
                    String v = props[0].getProperty(k);

                    global.put(gk, v);
                }
            }

        } catch (Exception e) {
            throw new Error("Can't load loginserver configuration", e);
        }
    }

}
